import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入组件路由
// import Login from '../views/Login.vue'
const Login = () => import(/* webpackChunkName:"Login_Home_User" */ '../views/Login.vue')

// import Home from '../views/Home.vue'
const Home = () => import(/* webpackChunkName:"Login_Home_User" */ '../views/Home.vue')

// import User from '../components/users/User.vue'
const User = () => import(/* webpackChunkName:"Login_Home_User" */ '../components/users/User.vue')

// import Roles from '../components/rights/Roles'
const Roles = () => import(/* webpackChunkName:"Roles" */ '../components/rights/Roles')

// import Rights from '../components/rights/Rights.vue'
const Rights = () => import(/* webpackChunkName:"Rights" */ '../components/rights/Rights.vue')

// import Goods from '../components/goods/Goods.vue'
const Goods = () => import(/* webpackChunkName:"Goods" */ '../components/goods/Goods.vue')

// import Add from '../components/goods/Add.vue'
const Add = () => import(/* webpackChunkName:"Add" */ '../components/goods/Add.vue')

// import Params from '../components/goods/Params.vue'
const Params = () => import(/* webpackChunkName:"Params" */ '../components/goods/Params.vue')

// import Cate from '../components/goods/Cate.vue'
const Cate = () => import(/* webpackChunkName:"Cate" */ '../components/goods/Cate.vue')

// import Orders from '../components/orders/Orders.vue'
const Orders = () => import(/* webpackChunkName:"Orders" */ '../components/orders/Orders.vue')

// import Reports from '../components/reports/Reports.vue'
const Reports = () => import(/* webpackChunkName:"Reports" */ '../components/reports/Reports.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {
    path: '/home',
    component: Home,
    redirect: '/users',
    children: [
      {path: '/users', component: User},
      {path: '/roles', component: Roles},
      {path: '/rights', component: Rights},
      {path: '/goods', component: Goods},
      {path: '/goods/add', component: Add},
      {path: '/params', component: Params},
      {path: '/categories', component: Cate},
      {path: '/orders', component: Orders},
      {path: '/reports', component: Reports}
    ]
  }

]

const router = new VueRouter({
  routes
})

// 全局前置防卫控制权限
router.beforeEach((to, from, next) => {
  // 如果用户访问的登录页，直接放行
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
